<?php
class Pageley_Basic_Command_Delete extends Pageley_Controller_Command_TransactionalReadWrite {

	private $id;
	private $ids;
	private $typeName;

	public function setId($id) {
		$this->id = $id;
	}

    public function setIds($ids) {
        $this->ids = $ids;
    }

	public function setTypeName($typeName) {
		$this->typeName = $typeName;
	}

	protected function executeInternal() {
		$unitOfWork = $this->getUnitOfWork();
        $factory = Pageley_Config::getInstance();
		$type = $factory->valueOf("type." . $this->typeName);
        if($this->ids !== null) {
            foreach($this->ids as $id) {
                $this->delete($unitOfWork, $type, $id);
            }
        } else {
            $this->delete($unitOfWork, $type, $this->id);
        }
		return new Pageley_Controller_ModelAndView();
	}
	
	private function delete($unitOfWork, $type, $id) {
	    $entity = $unitOfWork->find($type, $id);
        $unitOfWork->unregister($entity);
	}
}